import time
import tweepy
from tweepy import OAuthHandler

def getFollower(api, user_id, output_file, flag):
    if(flag == False):
        try:
            print user_id
            for page in tweepy.Cursor(api.followers_ids, id=user_id).pages():
                str1 = '\n'.join(str(e) for e in page)
                output_file.write(str1)
                flag = True
    #             ids.extend(page)
    #             print len(ids)
    #             print ids
    #         return ids
        except tweepy.TweepError, e:
            if str(e) == "[{u'message': u'Rate limit exceeded', u'code': 88}]":
                print 'Sleeping...%s' % user_id
                time.sleep(60*10) #Sleep for 5 minutes
                getFollower(api, user_id, output_file, flag)
            else:
                print '----' + str(e)

def main():
    consumer_key = '...'
    consumer_secret = '...'
    access_token = '...'
    access_token_secret = '...'
    
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    
    api = tweepy.API(auth)
    
    input_file = '/home/ivy/workspace/git/Social/Social/file/seed2.txt'
    output_file = '/home/ivy/workspace/git/Social/Social/file/user.txt'
    f = open(output_file, 'w')
    
    with open(input_file) as hello_file:
        for line in hello_file:
            getFollower(api, line, f, False)
            #Sleeping...21588316
            


if __name__ == '__main__':
    main() 
